﻿/*-----------------------------------------------
// File: BattleNetOver.cs
// Description: 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ProtoBuf.Message;

namespace Battle
{
    public delegate void OnBattleResult(bool isWin);
    public class BattleNetOver
    {

        public static void BattleOver(BattleType type)
        {
            var bo = CreateBattleOver(type);
            if (bo != null)
            {
                bo.Excute();
            }
        }

        public static void PlaybackOver()
        {
            var bo = CreatePlaybackBattleOver();
            bo.Excute();
        }

        public static void BattleOverCallback(bool result)
        {
            if (StaticData.curBattleData.isPlayBack)
            {
                ManagerController.Instance.BattleRecruitToRoom();
                AudioManager.StopMusic();
                return;
            }
            BattleManager.Instance.battlePanel.ShowResultConfirm(result);
        }

        public static void OnShowBattleBalance()
        {
            BattleManager.Instance.battlePanel.Hide();
            switch (StaticData.curBattleData.battleType)
            {
                case BattleType.PVE:
                    {
                        if (StaticData.isCompleteTask)
                        {
                            BattleManager.Instance.ShowBattleBalance(StaticData.isLastBattleWin, BattleType.PVE);
                        }
                        else
                        {
                            ManagerController.Instance.BattleToCopy();
                        }
                    }
                    break;
                case BattleType.FIRST:
                    {
                        FirstCheckPointUtil.CheckPoint(EnterGameCheckPointEnum.EnterGameBattleEnd);
                        ManagerController.Instance.storyManager.dialogManager.Show("dialog_1000", () => {
                            ManagerController.Instance.BattleToLogin();
                            FirstCheckPointUtil.CheckPoint(EnterGameCheckPointEnum.EnterGameBattleEndTalkEnd);
                        });
                    }
                    break;
                case BattleType.RECRUIT_SHOW:
                    {
                        ManagerController.Instance.BattleRecruitToRoom();
                        AudioManager.StopMusic();
                    }
                    break;
                case BattleType.PVP:
                case BattleType.PLUNDER:
                case BattleType.ACTIVITY:
                case BattleType.FRIEND_PVP:
                case BattleType.FIELD_PVP:
                case BattleType.FIELD_PVE:
                case BattleType.WANTED:
                case BattleType.RUSH:
                case BattleType.RUSH_BOSS:
                case BattleType.FIELD_BOSS:
                    {
                        BattleManager.Instance.ShowBattleBalance(StaticData.isLastBattleWin, StaticData.curBattleData.battleType);
                    }
                    break;
            }
        }

        private static IBattleOver CreatePlaybackBattleOver()
        {
             return new RecruitShowBattleOver(BattleOverCallback);
        }

        private static IBattleOver CreateBattleOver(BattleType type)
        {
            switch (type)
            {
                case BattleType.FIRST:
                    return new FirstBattleOver(BattleOverCallback);
                case BattleType.PVE:
                    return new PVEBattleOver(BattleOverCallback);
                case BattleType.PVP:
                    return new PVPBattleOver(BattleOverCallback);
                case BattleType.PLUNDER:
                    return new PlunderBattleOver(BattleOverCallback);
                case BattleType.ACTIVITY:
                    return new ActivityBattleOver(BattleOverCallback);
                case BattleType.FRIEND_PVP:
                    return new FriendPVPBattleOver(BattleOverCallback);
                case BattleType.FIELD_PVP:
                    return new FieldPVPBattleOver(BattleOverCallback);
                case BattleType.FIELD_PVE:
                    return new FieldPVEBattleOver(BattleOverCallback);
                case BattleType.FIELD_BOSS:
                    return new FieldPVEBossBattleOver(BattleOverCallback);
                case BattleType.WANTED:
                    return new WantedBattleOver(BattleOverCallback);
                case BattleType.RUSH:
                    return new RushBattleOver(BattleOverCallback);
                case BattleType.RUSH_BOSS:
                    return new RushBossBattleOver(BattleOverCallback);
                case BattleType.RECRUIT_SHOW:
                    return new RecruitShowBattleOver(BattleOverCallback);
                default:
                    return null;
            }


            /*
            switch (type)
            {
                case BattleType.FIRST:
                    return new FirstBattleOver();
                case BattleType.PVE:
                    return new PVEBattleOver();
                case BattleType.PVP:
                    return new PVPBattleOver();
                case BattleType.PLUNDER:
                    return new PlunderBattleOver();
                case BattleType.ACTIVITY:
                    return new ActivityBattleOver();
                case BattleType.FIELD_PVP:
                    return new FieldPVPBattleOver();
                case BattleType.FIELD_PVE:
                    return new FieldPVEBattleOver();
                case BattleType.WANTED:
                    return new WantedBattleOver();
                case BattleType.RUSH:
                    return new RushBattleOver();
                case BattleType.RUSH_BOSS:
                    return new RushBossBattleOver();
                default:
                    return null;
            }
            */

        }

        public static ProtoBuf.Message.BattleOperate ParseOptRecord() {
            ProtoBuf.Message.BattleOperate opt = new ProtoBuf.Message.BattleOperate();
            var engine = BattleManager.Instance.battleEngine;
            opt.finalRoundNum = engine.compute.data.roundNum;
            opt.attackerTotalLeftHp = engine.compute.data.GetNumHP(Engine.CampType.ATTACK);
            opt.defenderTotalLeftHp = engine.compute.data.GetNumHP(Engine.CampType.DEFENCE);
            List<Battle.Engine.LogicModel.FOperateSkill> attackerSkills;
            List<Battle.Engine.LogicModel.FOperateTrans> attackerTrans;
            List<Battle.Engine.LogicModel.FOperateSkill> defenderSkills;
            List<Battle.Engine.LogicModel.FOperateTrans> defenderTrans;
            BattleManager.Instance.battleEngine.GetRecordOperates(Engine.CampType.ATTACK, out attackerSkills, out attackerTrans);
            BattleManager.Instance.battleEngine.GetRecordOperates(Engine.CampType.DEFENCE, out defenderSkills, out defenderTrans);
            if (attackerSkills != null)
            {
                foreach (var skill in attackerSkills)
                {
                    ProtoBuf.Message.BattleSkillOperate skillOpt = new ProtoBuf.Message.BattleSkillOperate();
                    skillOpt.roundNum = skill.round;
                    skillOpt.roundTime = skill.roundTime;
                    skillOpt.camp = (int)skill.camp;
                    skillOpt.actorUid = skill.uniquedId.Split('_')[1];
                    skillOpt.skillIndex = skill.skillIndex;
                    opt.skillOpts.Add(skillOpt);
                }
            }
            if (defenderSkills != null) {
                foreach (var skill in defenderSkills)
                {
                    ProtoBuf.Message.BattleSkillOperate skillOpt = new ProtoBuf.Message.BattleSkillOperate();
                    skillOpt.roundNum = skill.round;
                    skillOpt.roundTime = skill.roundTime;
                    skillOpt.camp = (int)skill.camp;
                    skillOpt.actorUid = skill.uniquedId.Split('_')[1];
                    skillOpt.skillIndex = skill.skillIndex;
                    opt.skillOpts.Add(skillOpt);
                }
            }
            if (attackerTrans != null && attackerTrans.Count > 0) {
                opt.attackerTransOpt = new ProtoBuf.Message.BattleTransOperate();
                opt.attackerTransOpt.roundNum = attackerTrans[0].round;
                opt.attackerTransOpt.fpids.AddRange(attackerTrans[0].fpids);
                List<string> uids = new List<string>();
                foreach (var str in attackerTrans[0].uids)
                {
                    uids.Add(str.Split('_')[1]);
                }
                opt.attackerTransOpt.actorUids.AddRange(uids);
            }
            if (defenderTrans != null && defenderTrans.Count > 0) {
                opt.defenderTransOpt = new ProtoBuf.Message.BattleTransOperate();
                opt.defenderTransOpt.roundNum = defenderTrans[0].round;
                opt.defenderTransOpt.fpids.AddRange(defenderTrans[0].fpids);
                List<string> uids = new List<string>();
                foreach (var str in defenderTrans[0].uids)
                {
                    uids.Add(str.Split('_')[1]);
                }
                opt.defenderTransOpt.actorUids.AddRange(defenderTrans[0].uids);
            }
            return opt;
        }

    }
}